package com.lzq.leetcode.hoot100;

public class s5 {

    public String longestPalindrome(String s) {
        // 中心扩展法
        int l = 0, r = 0;
        for (int i=0; i<s.length(); i++) {
            int len1 = centerExpend(s, i, i);
            int len2 = centerExpend(s, i, i+1);
            int len = Math.max(len1, len2);
            if (len > (r - l + 1)) {
                l = i - (len - 1) / 2;
                r = i + len / 2;
            }
        }
        return s.substring(l, r + 1);
    }

    private int centerExpend(String s, int i, int j) {
        while (i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)) {
            --i;
            ++j;
        }
        return j - i - 1;
    }

    public static void main(String[] args) {
        s5 s5 = new s5();
        System.out.println(s5.longestPalindrome("cbbd"));
    }
}
